import requests
from lxml import etree
import re



referer = "referer: https://hz.zu.fang.com"
headers ={
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.9 Safari/537.36",
    "referer": referer,
    "sec-fetch-dest": "document",
    "sec-fetch-mode": "navigate",
    "sec-fetch-site": "same-origin",
    "sec-fetch-user": "?1",
    "upgrade-insecure-requests": "1"
}
url_start = "https://hz.zu.fang.com/house/i33/"
url_restart = "https://hz.zu.fang.com"
def get_info(url):
    global url_restart
    req = requests.get(url=url)
    # print(req.text)
    ele = etree.HTML(req.text)
    titles = ele.xpath("//p[@class='title']/a/text()")
    if not titles:
        url = ele.xpath("//a[@class='btn-redir']/@href")[0]
        get_info(url)
    styles = ele.xpath("//p[@class='font15 mt12 bold']/text()")
    locations = ele.xpath("//p[@class='gray6 mt12']//text()")
    moneys = ele.xpath("//p[@class='mt5 alingC']//text()")
    urls = ele.xpath("//div[@class='fanye']/a/@href")
    urls_text = ele.xpath("//div[@class='fanye']/a/text()")
    l = []
    l1 = []
    l2 = len(locations)
    for i in range(l2 // 5):
        l.append("".join(locations[i * 5:(i + 1) * 5]))
        l1.append("".join(styles[i * 4:(i+1)*4]))
    write_len  = len(titles)
    print(len(titles),titles)
    print(len(l1),l1)
    print(len(l),l)
    print(len(moneys),moneys)
    try:
        for k in range(write_len):
            d = "".join(re.findall("\w+",l1[k]))
            str = titles[k].strip() + " " + re.sub("O","㎡",d) + " " + l[k].strip()+ " " + moneys[k*2].strip()
            f.writelines(str+"\n")
    except Exception as e:
        print(e)
    print("已经写入了%d" % write_len)
    print(url_restart+urls[-2])
    while urls_text[-2]=="下一页":
        get_info(url_restart+urls[-2])
    else:
        return



if __name__ == "__main__":
    f = open('room.txt', 'a+', encoding="utf-8")
    get_info(url_start)
    f.close()